class Solution
{
public:
    ListNode* middleNode(ListNode* head)
    {
        auto slow = head;
        auto fast = head;
        while (fast && fast->next)
        {
            slow = slow->next;
            fast = fast->next->next;
        }
        return slow;
    }

    ListNode* reverseList(ListNode* head)
    {
        if (head == nullptr || head->next == nullptr)
            return head;

        ListNode* cur = reverseList(head->next);

        head->next->next = head;
        head->next = nullptr;
        return cur;
    }


    bool isPalindrome(ListNode* head)
    {
        auto mid = middleNode(head);
        auto head2 = reverseList(mid);

        while (head != mid)
        {
            if (head->val != head2->val)
                return false;
            head = head->next;
            head2 = head2->next;
        }
        return true;
    }
};